﻿using FileEncription;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;

namespace FileEncryptionTest
{
    
    
    /// <summary>
    ///This is a test class for EncryptionTest and is intended
    ///to contain all EncryptionTest Unit Tests
    ///</summary>
    [TestClass()]
    public class EncryptionTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


    

        /// <summary>
        ///A test for Encrypt
        ///</summary>
        [TestMethod()]
        public void EncryptTest()
        {
            byte[] input = new byte[] {245,168,34,195,254,52,86, 98};
            string fileName = "example.bin";
            File.Delete(fileName);
            FileStream test = new FileStream(fileName, FileMode.Create);
            using (test) 
            {
                test.Write(input, 0, input.Length);
            }
            Encryption.Encrypt(fileName);
            byte[] expected = new byte[] { 254, 245, 168, 34, 195, 98, 52, 86 };
            FileStream result = new FileStream("encrypted" + fileName, FileMode.Open);
            using (result) 
            {
                byte[] actual = new byte[result.Length];
                result.Read(actual, 0, actual.Length);
                for (int i = 0; i < actual.Length; i++)
                {
                    Assert.AreEqual(expected[i], actual[i]);
                }
            }
        }

        [TestMethod()]
        public void EncryptFileSizeTest()
        {
            byte[] input = new byte[] { 245, 168, 34, 195, 254, 52, 86, 98 };
            string fileName = "example.bin";
            File.Delete(fileName);
            FileStream test = new FileStream(fileName, FileMode.Create);
            using (test)
            {
                test.Write(input, 0, input.Length);
            }
            Encryption.Encrypt(fileName);
            FileStream result = new FileStream("encrypted" + fileName, FileMode.Open);
            using (result)
            {
                Assert.AreEqual(result.Length, input.Length);
            }
        }

        [TestMethod()]
        public void DecryptFileTest()
        {
            byte[] input = new byte[] { 245, 168, 34, 195, 254, 52, 86, 98 };
            string fileName = "example.bin";
            File.Delete(fileName);
            FileStream test = new FileStream(fileName, FileMode.Create);
            using (test)
            {
                test.Write(input, 0, input.Length);
            }
            string encrypted = Encryption.Encrypt(fileName);
            Encryption.Decrypt(encrypted);
            FileStream result = new FileStream("Decrypted", FileMode.Open);
            using (result)
            {
                Assert.AreEqual(result.Length, input.Length);
                byte[] actual = new byte[result.Length];
                result.Read(actual, 0, actual.Length);
                for (int i = 0; i < actual.Length; i++)
                {
                    Assert.AreEqual(input[i], actual[i]);
                }
            }
        }

        [TestMethod()]
        public void DecryptFileSizeTest()
        {
            byte[] input = new byte[] { 245, 168, 34, 195, 254, 52, 86, 98 };
            string fileName = "example.bin";
            File.Delete(fileName);
            FileStream test = new FileStream(fileName, FileMode.Create);
            using (test)
            {
                test.Write(input, 0, input.Length);
            }
            string encrypted = Encryption.Encrypt(fileName);
            Encryption.Decrypt(encrypted);
            FileStream result = new FileStream("Decrypted", FileMode.Open);
            using (result)
            {
                Assert.AreEqual(result.Length, input.Length);
            }
        }
    }
}
